import 'babel-core/polyfill'
import React from 'react'
import {render} from 'react-dom'
import {Provider} from 'react-redux'
import configureStore from './bin/modules/configureStore'
import {Router, Route, browserHistory, IndexRedirect, IndexRoute} from 'react-router'
import './bin/common/sass/public/standard.scss'
import './bin/common/sass/public/browser.scss'
import './bin/common/sass/public/search.scss'
import './bin/common/sass/public/table.scss'
import './bin/common/sass/public/customInput.scss'
const store = configureStore()
let rootElement = document.getElementById('root')

//三个函数都是异步加载，实现按需加载
const CustomRouter = {
    //根路径
    path: '/',
    //当前方法先匹配才进入，如果匹配不上就不进入
    onEnter: function (nextState, replaceState) {
        /*重定向：若是不需要获取页面数据进行判断，重定向最好在onEnter进行处理，提高性能*/
        /*主页显示活动视图，对/进行重定向到活动视图的路由*/
        /*if(nextState.location.pathname==='/'){
         replaceState(null, '/activityManage/activityView');
         }*/
        //如果未登录统一跳转登录
        //模拟已经未登录
        //设置cookie等认证信息，在每次刷新时不需要重新登录
        let flag = true;//true代表已经登录
        if (nextState.location.pathname != '/login' && !flag) {
            replaceState(null, '/login');
        } else if (nextState.location.pathname === '/') {
            replaceState(null, '/activity/view');
        }
    },
    onLeave: function (prevState) {
        console.log('离开：/');
    },
    //跟路径显示的内容
    getComponents(nextState, callback) {
        require.ensure([], function (require) {
            if ((nextState.location.pathname === '/login')) {
                callback(null, require('./bin/portal/header/Login_route'));
            } else {
                callback(null, require('./bin/portal/header/href_route'));
            }

        })
    },
    //默认/展示的路由
    // indexRoute: {component: require('./bin/modules/activity/containers/ActivityView')},

    //路径嵌套
    getChildRoutes(location, callback) {
        require.ensure([], function (require) {
            callback(null, [
                //效果评估
                require('./bin/modules/evaluate/routes'),
                //登录
                require('./bin/modules/login/routes'),
                //渠道
                require('./bin/modules/channel/routes'),
                //运营位
                require('./bin/modules/operp/routes'),
                //场景
                require('./bin/modules/scene/routes'),
                //策略
                require('./bin/modules/strategy/routes'),
                //活动
                require('./bin/modules/activity/routes'),
                //活动优先级
                require('./bin/modules/activity/routes/priorityIndex.js'),
                //个人推荐查询
                require('./bin/modules/recquery/routes'),
                {
                    path: '/Icons',
                    component: require('./bin/common/template/Icon/Icons')
                },
                require('./bin/modules/recquery/routes'),
                //跳转错误页面的路由（需要传递errorCode）
                {
                    path: '/error(/:errorCode)',
                    component: require('./bin/common/error/notFound')
                },
                //匹配任何路由，为了展示自动显示路由错误是的页面，请把此路由放到最后。。
                {
                    path: '*',
                    component: require('./bin/common/error/notFound')
                }
            ])
        })
    },
}
render(
    <Provider store={store}>
        <Router history={browserHistory} routes={CustomRouter}/>
    </Provider>,
    rootElement
)